package com.star.spring.filter.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.Objects;

/**
 * @ClassName MyIntecepter
 * @Author star
 * @Date 2024/8/28 17:45
 */
public class MyInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {


        String ip = request.getRemoteAddr();
        HttpSession session = request.getSession();
        Integer count = (Integer)session.getAttribute("count");
        count = Objects.isNull(count)?0:++count;
        session.setAttribute("count",count);

        System.out.println("ip"+ip + "访问次数为"+count);

        return true;
    }
}
